import os
from PyQt5.QtCore import QThread, \
                         pyqtSignal
from RcLogAnalyzer.scripts.rcg import rcg_to_db
from RcLogAnalyzer.scripts.log import LogAnalyzer
from RcLogAnalyzer.scripts.setting import global_setting


class ThreadLogAnalyze(QThread):
    trigger = pyqtSignal()
    processing_file_name = ''
    data_save_path = ''
    la = LogAnalyzer()
    analyze_result = {'lname': '', 'rname': '',
                      'lscore': 0, 'rscore': 0,
                      'l_pass_num': 0, 'r_pass_num': 0,
                      'l_intercept_num': 0, 'r_intercept_num': 0,
                      'l_dribble_num': 0, 'r_dribble_num': 0,
                      'l_long_pass': 0, 'r_long_pass': 0,
                      'l_normal_pass': 0, 'r_normal_pass': 0,
                      'l_short_pass': 0, 'r_short_pass': 0,
                      'l_through_pass': 0, 'r_through_pass': 0}

    def run(self):
        # 输出数据库文件
        if rcg_to_db(self.processing_file_name[:-1]+'g', self.data_save_path) == 0:
            print('output db file success')
        else:
            print('output fail')
        # 清除之前的分析结果
        self.la.clear()
        # 是否保存数据集
        if global_setting.log_output_dataset == 1:
            self.la.set_output_through_pass_dataset(1)
        else:
            self.la.set_output_through_pass_dataset(0)
        # 开始分析
        self.la.analyze(self.processing_file_name, self.data_save_path)

        self.analyze_result = self.la.analyze_result.copy()
        self.trigger.emit()
